package cn.miui.mall.controller;

import cn.miui.mall.pojo.product_types;
import cn.miui.mall.pojo.userinfo;
import cn.miui.mall.service.Typeservice;
import cn.miui.mall.service.Userservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/User")
public class UserController {
    @Autowired
    private Userservice userservice;


    /***
     * 登陆方法(验证码后补)
     * @param username
     * @param password
     * @return 主页面
     */
    @RequestMapping("/login")
    public ModelAndView login(HttpServletRequest request, String username, String password){
        userinfo userinfoList=userservice.findByusernameAndUserpassword(username,password);
        HttpSession session=request.getSession();
        if (userinfoList!=null){
            session.setAttribute("sessionname",userinfoList.getUsername());
            session.setAttribute("sessionuserid",userinfoList.getUserid());
            return new ModelAndView("redirect:/findAll?id="+userinfoList.getUserid());//用户存在
        }
        return new ModelAndView("login");
    }

    /**
     * 注册(手机验证码后补)
     * @param user
     * @return
     */
    @RequestMapping("/register")
    public ModelAndView register(userinfo user){
       userservice.sava(user);
        return new ModelAndView("login");
    }
    /***
     * 个人中心
     */
    @RequestMapping("self_info")
    public ModelAndView self_info(Integer id,Map<String,Object> map){
        map.put("userinfo",userservice.findbyId(id));
        return new ModelAndView("self_info",map);
    }
}

